**********************************************
* VIOLENT COMPETITION AND TERRORIST RESTRAINT
* 
* Sara M.T. Polo (University of Essex)
* sara.polo@essex.ac.uk
*
* Blair Welsh (NYU - Abu Dhabi)
* blairwelsh@nyu.edu
*
* International Organization, 2024
**********************************************

* Change to respective directory
cd "/Users/bjw9936/Desktop/Polo_Welsh_IO/01_Main"

**********************************************
* FIGURE 1 & 2
**********************************************

use figure1.dta, clear
graph hbar infight, over(gtdname, sort(1)) ytitle("Number of Attacks on Other Organizations", size(small)) bar(1, fcolor(black)) ylabel(, labsize(small) nogrid)

use figure2a.dta, clear
twoway (bar deaths date, sort ytitle("") xtitle("") tlabel(1989m1(48)2018m12) xlabel(, grid) ylabel(, grid) color(navy) title("Non-State Conflict Fatalities") graphregion(color(white)))

use figure2b.dta, clear
twoway (bar infight date, sort ytitle("") xtitle("") tlabel(1989m1(48)2018m12) xlabel(, grid) ylabel(, grid) color(navy) title("Terrorist Infighting Attacks") graphregion(color(white)))

**********************************************
* MACRO-LEVEL ANALYSIS
**********************************************

* Set up
use main.dta, clear
xtset newid iyear
set scheme s1color

* Summary Statistics (Table 1)
summarize softratio infight infight_b ln_infight nat left right rel regimetype foreignspon territory attacks n_group ch_group softratio_lag size ln_gdp ln_pop pts_a efindex

* Infighting as Binary Variable (Models 1-3, Table 2)
glm softratio infight_b, family(binomial) link(logit) cluster(gtdname)
glm softratio infight_b nat left right rel regimetype foreignspon territory attacks n_group ch_group conflictyear softratio_lag, family(binomial) link(logit) cluster(gtdname)
glm softratio infight_b nat left right rel regimetype foreignspon territory attacks n_group ch_group conflictyear softratio_lag size ln_gdp ln_pop pts_a efindex, family(binomial) link(logit) cluster(gtdname)

* Infighting as (Logged) Count (Models 4-6, Table 2)
glm softratio ln_infight, family(binomial) link(logit) cluster(gtdname)
glm softratio ln_infight nat left right rel regimetype foreignspon territory attacks n_group ch_group conflictyear softratio_lag, family(binomial) link(logit) cluster(gtdname)
glm softratio ln_infight nat left right rel regimetype foreignspon territory attacks n_group ch_group conflictyear softratio_lag size ln_gdp ln_pop pts_a efindex, family(binomial) link(logit) cluster(gtdname)

* Predicted Proportions (Figure 3)
* Presence of Violent Competition (Figure 3a)
quietly glm softratio i.infight_b nat left right rel regimetype foreignspon territory attacks n_group ch_group conflictyear softratio_lag, family(binomial) link(logit) cluster(gtdname)

margins i.infight_b, post
coefplot (., keep(0.infight_b) bcolor(navy)) ///
		 (., keep(1.infight_b) bcolor(bluishgray)) /// 
		 , vertical legend(off) nooffsets recast(bar) barwidth(0.8) fcolor(*.8) ///
	   citop ciopts(recast(rcap) lcol(black)) citype() ///
       coeflabels(, notick labgap(2)) plotregion(margin(b=0)) ///
	   ylabel(0(0.1)0.7) ytitle(Predicted Proportion of Attacks on Soft Targets) ///
	   xlabel(1 "No Violent Competition" 2 "Violent Competition")  ///
	   addplot(scatter @b @at, ms(i) mlabel(@b) mlabformat(%4.3f) mlabpos(2) mlabcolor(black))
	   

* Intensity of Violent Competition (Figure 3b)
quietly glm softratio c.ln_infight nat left right rel regimetype foreignspon territory attacks n_group ch_group conflictyear softratio_lag, family(binomial) link(logit) cluster(gtdname)

margins, at(ln_infight=(0(1)10))
marginsplot, recast(line) recastci(rarea) ///
ciopt(color(bluishgray)) plot1opts(lc(navy) lpattern(solid)) ///
ytitle("Predicted Proportion of Attacks on Soft Targets") ///
xtitle("Infighting Attacks (log)") ///
ylab(0(0.1)0.7, nogrid) title("")

**********************************************
* MICRO-LEVEL ANALYSIS
**********************************************

* Figure 4
use figure4.dta, clear
sort date
twoway (bar softratio date, sort fcolor(ltblue%80) lcolor(ltblue)) (line infight date, sort yaxis(2) lcolor(navy)), xtitle("") ytitle("Proportion of ISIS Attacks on Soft Targets") ytitle("ISIS Infighting Attacks", axis(2)) legend(label(1 "Soft Ratio") label(2 "Infighting Attacks"))

* Set up
use syria.dta, clear
xtset id modate

* Linear Model with District and Temporal FE (Table 3)
xtreg softratio infight_b softratiolag i.iyear, fe vce(cluster id)
xtreg softratio infight_b deathsb attacks territory tax_index services_index softratiolag i.iyear, fe vce(cluster id)


* Predicted Proportions (Figure 5)
quietly xtreg softratio i.infight_b deathsb attacks territory tax_index services_index softratiolag i.iyear, fe vce(cluster id)

margins i.infight_b, post
coefplot (., keep(0.infight_b) bcolor(navy)) ///
		 (., keep(1.infight_b) bcolor(bluishgray)) /// 
		 , vertical legend(off) nooffsets recast(bar) barwidth(0.8) fcolor(*.8) ///
	   citop ciopts(recast(rcap) lcol(black)) citype() ///
       coeflabels(, notick labgap(2)) plotregion(margin(b=0)) ///
	   ylabel(0(0.1)0.7) ytitle(Predicted Proportion of Attacks on Soft Targets) ///
	   xlabel(1 "No Violent Competition" 2 "Violent Competition")  ///
	   addplot(scatter @b @at, ms(i) mlabel(@b) mlabformat(%4.3f) mlabpos(2) mlabcolor(black))  

**********************************************
* EXTENSIONS
**********************************************

* In-group and Out-group Dynamics (Figure 6)
* Sunni Locations (Figure 6a)
quietly xtreg softratio i.infight_b deathsb attacks territory tax_index services_index softratiolag i.iyear, fe vce(cluster id), if sunni == 1
margins i.infight_b, post
coefplot, vertical legend(off) nooffsets recast(bar) barwidth(0.8) fcolor(*.8) ///
       bcolor(navy) ///
	   citop ciopts(recast(rcap) lcol(black)) citype() ///
       coeflabels(, notick labgap(2)) plotregion(margin(b=0)) ///
	   ylabel(0(0.1)0.7) ytitle(Predicted Proportion of Attacks on Soft Targets) ///
	   xlabel(1 "No Violent Competition" 2 "Violent Competition")  ///
	   addplot(scatter @b @at, ms(i) mlabel(@b) mlabformat(%4.3f) mlabpos(2) mlabcolor(black)) 

* Non-Sunni and Mixed Locations (Figure 6b)
quietly xtreg softratio i.infight_b deathsb attacks territory tax_index services_index softratiolag i.iyear, fe vce(cluster id), if mixed2 | nonsunni == 1
margins i.infight_b, post
coefplot, vertical legend(off) nooffsets recast(bar) barwidth(0.8) fcolor(*.8) ///
       bcolor(bluishgray) ///
	   citop ciopts(recast(rcap) lcol(black)) citype() ///
       coeflabels(, notick labgap(2)) plotregion(margin(b=0)) ///
	   ylabel(0(0.1)0.7) ytitle(Predicted Proportion of Attacks on Soft Targets) ///
	   xlabel(1 "No Violent Competition" 2 "Violent Competition")  ///
	   addplot(scatter @b @at, ms(i) mlabel(@b) mlabformat(%4.3f) mlabpos(2) mlabcolor(black))
	   
* Brutality of Competitors (Figure 7)
quietly xtreg soft c.infight##c.soft_comp deaths_is deathsb attacks territory tax_index services_index softratiolag i.iyear, fe vce(cluster id)
quietly margins, at(infight=(0(1)7) soft_comp=(0(5)25)) saving(predictions, replace)

use predictions, clear
rename _at1 infight
rename _at2 soft_comp
rename _margin pr_soft

twoway  (contour pr_soft infight soft_comp, crule(intensity) ecolor(navy)), xlabel(0(5)25) ylabel(0(1)7, angle(horizontal)) xtitle("Attacks on Soft Targets by ISIS Competitors") ytitle("Infighting") ztitle("Predicted Attacks on Soft Targets by ISIS") title("")

